package org.bangumibuddy.mock.httpserver.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import lombok.SneakyThrows;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RSSHandler implements HttpHandler{
    @Override
    @SneakyThrows
    public void handle(HttpExchange exchange) throws IOException {
        // Step 1: Get the request URI
        String requestPath = exchange.getRequestURI().getPath();

        // Read the file content
        byte[] fileContent = Files.readAllBytes(Path.of(getClass().getResource(requestPath).toURI()));

        // Set the response headers
        exchange.getResponseHeaders().set("Content-Type", "application/xml");
        exchange.sendResponseHeaders(200, fileContent.length);

        // Write the file content to the response body
        OutputStream responseBody = exchange.getResponseBody();
        responseBody.write(fileContent);

        // Close the output stream and the exchange
        responseBody.close();
    }
}
